/*
 * @Description: vite.config
 * @Autor: lcz
 * @LastEditors: lcz
 * @LastEditTime: 2023-10-16 15:31:50
 */
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import legacy from "@vitejs/plugin-legacy";
import progress from "vite-plugin-progress";

import BuryingPoint from "./plugin/buryingPoint";
import InjectConst from "./plugin/injectConst";
import study from "./plugin/study";
import * as path from "path";
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  // 设置第三个参数为 '' 来加载所有环境变量，而不管是否有 `VITE_` 前缀。
  const env: Partial<ImportMetaEnv> = loadEnv(mode, process.cwd(), "");

  return {
    base: "/", //默认/ 与静态服务器存储地址相关
    publicDir: "public",
    build: {
      outDir: "dist",
      assetsDir: "assets",
      sourcemap: false,
      copyPublicDir: true, //拷贝public目录到dist下
      cssCodeSplit: false, // false CSS 将被提取到一个 CSS 文件中, true CSS 代码拆分,合一起重复的会下,但是请求加载第一次会慢
      manifest: true, //构建后将会生成 manifest.json 文件
      reportCompressedSize: true, //gzip 压缩大小报告  大型项目禁用
      chunkSizeWarningLimit: 1024, // 触发警告的 chunk 大小,后进行优化
      assetsInlineLimit: 1024, //小于此阈值的导入或引用资源将内联为 base64 编码
      rollupOptions: {
        output: {
          entryFileNames: `assets/[name].[hash].js`,
          chunkFileNames: `assets/[name].[hash].js`,
          assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
          compact: true, // 压缩roullup产生的额外代码
          manualChunks: {}
        }
      }
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
        "@components": path.resolve(__dirname, "src/components"),
        "@layouts": path.resolve(__dirname, "src/layouts"),
        "@style": path.resolve(__dirname, "src/style"),
        "@utils": path.resolve(__dirname, "src/utils"),
        "@assets": path.resolve(__dirname, "src/assets"),
        "@hooks": path.resolve(__dirname, "src/hooks")
      }
      // 导入时想要省略的扩展名列表。
      // extensions: [".mjs", ".js", ".mts", ".ts", ".jsx", ".tsx", ".json"]
    },
    plugins: [
      react(),
      progress(),
      {
        ...legacy({
          targets: ["defaults", "not IE 11"]
        }),
        apply: "build", //'build' 构建的时候才会调用 'serve'
        enforce: "post" // 'pre' 'post' 在vite 核心插件之后调用
      },

      {
        ...BuryingPoint({
          // include: path.resolve("src/main.tsx")
          include: path.resolve("src/views/testPages/index.tsx") // or you
        }),
        // apply: "build", 
        enforce: "pre"
      },
      InjectConst(),
      
      // study()
    ],
    server: {
      // host: '0.0.0.0',
      proxy: {
        [env.VITE_APP_BASE_URL]: {
          target: "http://dev.hz.bjdv.com:59000", // 后端服务器地址
          // target: "https://api.angineglobal.com", // 后端服务器地址
          changeOrigin: true, // 是否改变请求源地址
          rewrite: path => path.replace(/^\/api/, "") // 重写请求路径，去掉 '/api' 前缀
        }
        // '/socket.io': {
        //   target: 'ws://localhost:5174',
        //   ws: true,
        // },
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "./src/styles/global.scss";`
        }
      },
      devSourcemap: false //在开发过程中是否启用 sourcemap。
    },
    optimizeDeps: {
      // 依赖优化选项
      include: ["axios", "ahooks", "react-redux"] //强制预构建链接的包
    }
  };
});
